package com.tinckay.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tinckay.common.annotation.Log;
import com.tinckay.common.constant.ProjectConstant;
import com.tinckay.entity.po.StoModel;
import com.tinckay.service.StoModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.api.ApiController;

import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Jean
 * @since 2018-10-21
 */
@Api(tags = "仓体模型管理")
@RestController
@RequestMapping("/stoModel")
public class StoModelController extends ApiController {

    @Autowired
    private StoModelService stoModelService;

    @Log("删除仓体模型")
    @DeleteMapping
    @ApiOperation("删除仓体模型")
    //@RequiresPermissions("stoModel:delete")
    public R delete(@ApiParam("仓体模型id数组") @RequestParam Long[] ids) {
        if (ArrayUtil.isEmpty(ids)) {
            return failed("参数id不能为空.");
        }
        stoModelService.removeByIds(CollectionUtil.toList(ids));
        return success(null);
    }

    @Log("新增仓体模型")
    @PostMapping
    @ApiOperation("新增仓体模型")
    //@RequiresPermissions("stoModel:add")
    public R add(@RequestBody @Validated(ProjectConstant.Save.class) StoModel stoModel) {
        stoModelService.save(stoModel);
        return success(stoModel);
    }

    @Log("更新仓体模型")
    @PutMapping
    @ApiOperation("更新仓体模型")
    //@RequiresPermissions("stoModel:update")
    public R update(@RequestBody @Validated(ProjectConstant.Update.class) StoModel stoModel) {
        stoModelService.updateById(stoModel);
        return success(stoModel);
    }

    @GetMapping("/list")
    @ApiOperation("分页查询仓体模型列表")
    //@RequiresPermissions("stoModel:list")
    public R<IPage> list(@RequestParam(defaultValue = "1") int current,
                         @RequestParam(defaultValue = "10") int size,
                         @RequestParam(required = false) String name,
                         @RequestParam(required = false) String type) {
        LambdaQueryWrapper<StoModel> wrapper = new QueryWrapper<StoModel>().lambda()
                .eq(StrUtil.isNotBlank(name), StoModel::getName, name)
                .eq(StrUtil.isNotBlank(type), StoModel::getType, type);

        IPage<StoModel> page = stoModelService.page(new Page<>(current, size), wrapper);
        return success(page);
    }

    @GetMapping("/info/{stoModelId}")
    @ApiOperation("获取仓体模型信息")
    //@RequiresPermissions("stoModel:info")
    public R<StoModel> info(@PathVariable("stoModelId") long stoModelId) {
        return success(stoModelService.getById(stoModelId));
    }

}

